<template>
  <el-header class="header search-header mt20 mb20">
    <el-form
      ref="ruleFormRef"
      class="between-form"
      :model="search"
      :inline="true"
    >
      <div>
        <el-form-item label="企业名称" prop="enterpriseNameCn">
          <el-input
            v-model="search.enterpriseNameCn"
            placeholder="请输入企业名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="检查日期" prop="jcrq">
          <el-date-picker
            style="width: 210px"
            v-model="search.jcrq"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="检查类型" prop="inspectionType">
          <el-select
            v-model="search.inspectionType"
            placeholder="请选择"
            style="min-width: 120px"
            clearable
          >
            <el-option key="" label="全部" value="" />
            <el-option
              v-for="item in ['检查']"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="search.status"
            placeholder="请选择"
            style="min-width: 120px"
            clearable
          >
            <el-option key="" label="全部" value="" />
            <el-option
              v-for="item in ['已整改', '未整改']"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检查人员" prop="inspector">
          <el-input
            v-model="search.inspector"
            placeholder="请输入检查人员"
            clearable
          />
        </el-form-item>
      </div>

      <el-form-item class="mr0">
        <el-button
          type="primary"
          class="search-btn"
          @click="getSearch(ruleFormRef)"
        >
          查询
        </el-button>
        <el-button @click="reset(ruleFormRef)" class="reset-btn"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </el-header>
  <el-main>
    <el-container>
      <el-main>
        <el-container>
          <el-main class="container bc-table">
            <el-table
              :data="objecInfo.tableData"
              style="width: 100%; height: 100%"
            >
              <el-table-column
                prop="enterpriseNameCn"
                min-width="150px"
                label="企业名称"
              />
              <el-table-column prop="inspectionType" label="检查类型" />
              <el-table-column prop="inspectionDate" label="检查日期" />
              <el-table-column prop="status" label="状态" />
              <el-table-column prop="issuesCount" label="隐患问题数量">
              </el-table-column>
              <!-- <el-table-column prop="dataAmount" label="整改期限" /> -->
              <el-table-column prop="inspector" label="检查人员" />
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    link
                    type="primary"
                    size="small"
                    @click="gotodetail(scope.row)"
                    >查看</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-main>
          <el-footer class="footer mt20">
            <el-pagination
              v-model:current-page="tableField.pageNo"
              v-model:page-size="tableField.pageSize"
              :page-sizes="[10, 20, 30, 50, 100]"
              layout="sizes, prev, pager, next, jumper"
              :total="total"
              :background="true"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              prev-text="上一页"
              next-text="下一页"
            />
          </el-footer>
        </el-container>
      </el-main>
    </el-container>
  </el-main>

  <!-- 检查记录详情 -->
  <SelfDialog
    :dialogVisable="addVisable"
    title="检查记录详情"
    width="917px"
    @close="gotodetail()"
    @confirm="gotodetail()"
    :showFooter="false"
  >
    <template #default>
      <div class="center_content">
        <detail :currentRow="currentRowId"></detail>
      </div>
    </template>
  </SelfDialog>
</template>

<script setup>
import { onMounted, ref, reactive, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { getTypeList, busioptions } from '@/api';
import { getsalfList } from '@/api/dazx';
import { Minxfun } from '@/utils/minxin';
import SelfDialog from '@/components/dialog/dialog.vue';
import detail from './detail.vue';
import dayjs from 'dayjs';
const {
  handleSizeChange,
  handleCurrentChange,
  tableField,
  total,
  loading,
  formatDateTime,
} = Minxfun();
const router = useRouter();

const ruleFormRef = ref();
const search = reactive({
  inspector: '',
  inspectionType: '',
  status: '',
  jcrq: [],
  enterpriseNameCn: '',
});

onMounted(() => {
  // getType();
  getTableData();
});

// 获取部门
let doorList = ref([]);
let sortList = ref([]);
const getType = async () => {
  try {
    let res = await getTypeList({
      orgTypeCode: 'office',
    });
    doorList.value = res.data;
    let res1 = await busioptions();
    sortList.value = res1.data;
  } catch (e) {
    console.log(e);
  }
};

const getSearch = () => {
  if (tableField.value.pageNo == 1) {
    getTableData();
  } else {
    tableField.value.pageNo = 1;
    getTableData();
  }
};

const reset = (ruleFormRef) => {
  ruleFormRef.resetFields();
  getTableData();
};

watch(
  tableField,
  (newVal, oldVal) => {
    getTableData();
  },
  { deep: true }
);
let objecInfo = reactive({
  tableData: [],
});
const getTableData = async () => {
  try {
    loading.value = true;
    let { data } = await getsalfList({
      enterpriseType: '1',
      ...search,
      ...tableField.value,
      pageNum: tableField.value.pageNo,

      inspectionDateBegin:
        search.jcrq && search.jcrq.length > 0
          ? dayjs(search.jcrq[0]).format('YYYY-MM-DD')
          : '',
      inspectionDateEnd:
        search.jcrq && search.jcrq.length > 0
          ? dayjs(search.jcrq[1]).format('YYYY-MM-DD')
          : '',
    });
    loading.value = false;
    console.log({
      ...tableField.value,
      ...search,
      pageNum: tableField.value.pageNo,
    });
    objecInfo.tableData = data.inspectList;
    console.log(objecInfo.tableData);
    total.value = Number(data.total) || 0;
  } catch (e) {
    loading.value = false;
    console.log(e);
  }
};

let currentRow = ref(null);
let showDetail = ref(false);
const handleClick = (value) => {
  currentRow.value = value;
  showDetail.value = true;
};

// 导出
const saveExport = async () => {
  try {
    // exportVisable.value = false;
    console.log(fieldList.value);
    let params = {
      objectId: currentTab.value,
      queryOptions: searchData.value,
      orderBys: sortList.value,
    };
    let config = {
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Access-Control-Allow-Origin': true,
        withCredentials: true,
        'Access-Control-Expose-Headers': 'Authorization',
      },
      responseType: 'blob',
    };
    axios
      .post('/data-center/query/composite/export', params, config)
      .then((res) => {
        window.open(res.data.data.fileUrl);
        // const content = res.data;
        // const blob = new Blob([content]);
        // const fileName =
        //   decodeURI(
        //     res.headers["content-disposition"].split(";")[1].split("=")[1]
        //   ) || "文件.xlsx";
        // if ("download" in document.createElement("a")) {
        //   // 非IE下载
        //   const elink = document.createElement("a");
        //   elink.download = fileName;
        //   elink.style.display = "none";
        //   elink.href = URL.createObjectURL(blob);
        //   document.body.appendChild(elink);
        //   elink.click();
        //   URL.revokeObjectURL(elink.href); // 释放URL 对象
        //   document.body.removeChild(elink);
        // } else {
        //   // IE10+下载
        //   navigator.msSaveBlob(blob, fileName);
        // }
      });
  } catch (e) {}
};

// 展示详情
let showlist = ref(true);
let currentData = ref(null);
const changeStatus = (value) => {
  if (value) {
    currentData.value = value;
  } else {
    currentData.value = null;
  }
  showlist.value = !showlist.value;
};

// 关闭详情
const close = () => {
  showlist.value = !showlist.value;
};

// 展示详情
let addVisable = ref(false);
let currentRowId = ref(null);
const gotodetail = (row) => {
  addVisable.value = !addVisable.value;
  if (row) {
    currentRowId.value = row;
  }
};
</script>

<style lang="scss" scoped>
.home-qyda {
  width: 100%;
  height: 100%;
  .header {
    min-height: 36px;
    line-height: 36px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    overflow: hidden;
    margin-bottom: 18px;
    .header-l {
      display: flex;
      align-items: center;
      .text {
        font-family: PingFangSC, PingFang SC;
        font-weight: 600;
        font-size: 16px;
        color: #333333;
        line-height: 22px;
        letter-spacing: 1px;
        text-align: left;
        padding-left: 14px;
      }
    }
  }
  .container {
  }
}
.el-container {
  height: 100% !important;
  padding: 0 !important;
}
.el-header {
  height: auto !important;
  min-height: 36px !important;
}
.el-main {
  padding: 0 !important;
}
.search {
  margin: 0 50px;
}
.header-r {
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 14px;
  color: #666666;
}
</style>

<style>
.el-form-item__label {
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 14px;
  color: #333333;
}
</style>
